﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Permission.aspx.cs" Inherits="WEB.APP.Modules.System.Permission" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        function DocumentReady() {

            MethodNamespace.DocumentReady({
                success: function() {
                    storeMain.load();
                }
            });
        }

        function CheckEdit(el) {
            if (!hfIsEdit.value) {
                MethodNamespace.AlertPerEdit();
                return false;
            }
            if (el.field !== "IsView" && !el.record.data.IsView) {
                return false;
            }
            return true;
        }

        function CheckDataAll() {
            var items = storeMain.data.items;
            var IsView = true;
            var IsAdd = true;
            var IsEdit = true;
            var IsDelete = true;
            var IsPrint = true;
            var IsImport = true;
            var IsExport = true;
            for (i = 0; i < items.length; i++) {
                if (items[i].data.IsView === false) {
                    IsView = false;
                    break;
                }
                if (items[i].data.IsAdd === false) {
                    IsAdd = false;
                }
                if (items[i].data.IsAdd === false) {
                    IsAdd = false;
                }
                if (items[i].data.IsEdit === false) {
                    IsEdit = false;
                }
                if (items[i].data.IsDelete === false) {
                    IsDelete = false;
                }
                if (items[i].data.IsPrint === false) {
                    IsPrint = false;
                }
                if (items[i].data.IsImport === false) {
                    IsImport = false;
                }
                if (items[i].data.IsExport === false) {
                    IsExport = false;
                }
            }
            document.getElementById("chkViewAll").checked = IsView;
            if (IsView === true) {
                document.getElementById("chkAddAll").checked = IsAdd;
                document.getElementById("chkEditAll").checked = IsEdit;
                document.getElementById("chkDeleteAll").checked = IsDelete;
                document.getElementById("chkPrintAll").checked = IsPrint;
                document.getElementById("chkImportAll").checked = IsImport;
                document.getElementById("chkExportAll").checked = IsExport;
            }
            else {
                document.getElementById("chkAddAll").checked = false;
                document.getElementById("chkEditAll").checked = false;
                document.getElementById("chkDeleteAll").checked = false;
                document.getElementById("chkPrintAll").checked = false;
                document.getElementById("chkImportAll").checked = false;
                document.getElementById("chkExportAll").checked = false;
            }
        }

        function setViewAll(el) {
            if (!hfIsEdit.value) {
                document.getElementById("chkViewAll").checked = !document.getElementById("chkViewAll").checked;
                MethodNamespace.AlertPerEdit();
                return;
            }
            var chkViewAll = document.getElementById("chkViewAll");
            var checked = chkViewAll.checked;
            var items = grdMain.store.data.items;
            if (checked === false) {
                document.getElementById("chkAddAll").checked = checked;
                document.getElementById("chkEditAll").checked = checked;
                document.getElementById("chkDeleteAll").checked = checked;
                document.getElementById("chkPrintAll").checked = checked;
                document.getElementById("chkImportAll").checked = checked;
                document.getElementById("chkExportAll").checked = checked;

                for (i = 0; i < items.length; i++) {
                    setViewAllData(items[i].data, checked);
                }
            }
            else {
                for (i = 0; i < items.length; i++) {
                    items[i].data.IsView = checked;
                }
            }
            grdMain.store.commitChanges();
            MethodNamespace.UpdateViewAll(checked);
            grdMain.view.refresh();
        }
        function setViewAllData(item, checked) {
            item.IsView = checked;
            item.IsAdd = checked;
            item.IsEdit = checked;
            item.IsDelete = checked;
            item.IsPrint = checked;
            item.IsImport = checked;
            item.IsExport = checked;
        }
        function setAddAll() {
            if (!hfIsEdit.value) {
                document.getElementById("chkAddAll").checked = !document.getElementById("chkAddAll").checked;
                MethodNamespace.AlertPerEdit();
                return;
            }
            var chkAll = document.getElementById("chkAddAll");
            var checked = chkAll.checked;
            if (document.getElementById("chkViewAll").checked === false) {
                chkAll.checked = false;
                return;
            }
            var items = grdMain.store.data.items;

            for (i = 0; i < items.length; i++) {
                items[i].data.IsAdd = checked;
            }
            grdMain.store.commitChanges();
            MethodNamespace.UpdateAddAll(checked);
            grdMain.view.refresh();
        }
        function setEditAll() {
            if (!hfIsEdit.value) {
                document.getElementById("chkEditAll").checked = !document.getElementById("chkEditAll").checked;
                MethodNamespace.AlertPerEdit();
                return;
            }
            var chkAll = document.getElementById("chkEditAll");
            var checked = chkAll.checked;
            if (document.getElementById("chkViewAll").checked === false) {
                chkAll.checked = false;
                return;
            }
            var items = grdMain.store.data.items;

            for (i = 0; i < items.length; i++) {
                items[i].data.IsEdit = checked;
            }
            grdMain.store.commitChanges();
            MethodNamespace.UpdateEditAll(checked);
            grdMain.view.refresh();
        }
        function setDeleteAll() {
            if (!hfIsEdit.value) {
                document.getElementById("chkDeleteAll").checked = !document.getElementById("chkDeleteAll").checked;
                MethodNamespace.AlertPerEdit();
                return;
            }
            var chkAll = document.getElementById("chkDeleteAll");
            var checked = chkAll.checked;
            if (document.getElementById("chkViewAll").checked === false) {
                chkAll.checked = false;
                return;
            }
            var items = grdMain.store.data.items;

            for (i = 0; i < items.length; i++) {
                items[i].data.IsDelete = checked;
            }
            grdMain.store.commitChanges();
            MethodNamespace.UpdateDeleteAll(checked);
            grdMain.view.refresh();
        }
        function setPrintAll() {
            if (!hfIsEdit.value) {
                document.getElementById("chkPrintAll").checked = !document.getElementById("chkPrintAll").checked;
                MethodNamespace.AlertPerEdit();
                return;
            }
            var chkAll = document.getElementById("chkPrintAll");
            var checked = chkAll.checked;
            if (document.getElementById("chkViewAll").checked === false) {
                chkAll.checked = false;
                return;
            }
            var items = grdMain.store.data.items;

            for (i = 0; i < items.length; i++) {
                items[i].data.IsPrint = checked;
            }
            grdMain.store.commitChanges();
            MethodNamespace.UpdatePrintAll(checked);
            grdMain.view.refresh();
        }
        function setImportAll() {
            if (!hfIsEdit.value) {
                document.getElementById("chkImportAll").checked = !document.getElementById("chkImportAll").checked;
                MethodNamespace.AlertPerEdit();
                return;
            }
            var chkAll = document.getElementById("chkImportAll");
            var checked = chkAll.checked;
            if (document.getElementById("chkViewAll").checked === false) {
                chkAll.checked = false;
                return;
            }
            var items = grdMain.store.data.items;

            for (i = 0; i < items.length; i++) {
                items[i].data.IsImport = checked;
            }
            grdMain.store.commitChanges();
            MethodNamespace.UpdateImportAll(checked);
            grdMain.view.refresh();
        }
        function setExportAll() {
            if (!hfIsEdit.value) {
                document.getElementById("chkExportAll").checked = !document.getElementById("chkExportAll").checked;
                MethodNamespace.AlertPerEdit();
                return;
            }
            var chkAll = document.getElementById("chkExportAll");
            var checked = chkAll.checked;
            if (document.getElementById("chkViewAll").checked === false) {
                chkAll.checked = false;
                return;
            }
            var items = grdMain.store.data.items;

            for (i = 0; i < items.length; i++) {
                items[i].data.IsExport = checked;
            }
            grdMain.store.commitChanges();
            MethodNamespace.UpdateExportAll(checked);
            grdMain.view.refresh();
        }

        function setViews(el) {
            var data = el.record.data;
            var field = el.field;
            if (el.field === "IsView" && !data.IsView) {
                data.IsAdd = data.IsView;
                data.IsEdit = data.IsView;
                data.IsDelete = data.IsView;
                data.IsPrint = data.IsView;
                data.IsImport = data.IsView;
                data.IsExport = data.IsView;
                el.grid.store.commitChanges();
            }
            CheckDataAll();
        }

        function CheckAllRows(grid, Check) {
            if (!hfIsEdit.value) {
                MethodNamespace.AlertPerEdit();
                return false;
            }
            var _temp = grid.selModel.selections.items
            for (var i = 0; i < _temp.length; i++) {
                var _Data = grid.store.data.item(_temp[i].id).data;
                _Data.IsView = Check;
                _Data.IsAdd = Check;
                _Data.IsEdit = Check;
                _Data.IsDelete = Check;
                _Data.IsPrint = Check;
                _Data.IsImport = Check;
                _Data.IsExport = Check;
                MethodNamespace.CheckAllRow(_Data.ScreenId, _Data.RoleId, Check);
            }
            grid.store.commitChanges();
            grid.view.refresh();
            CheckDataAll();
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <ext:Hidden runat="server" ID="hfIsEdit" />
    <ext:Store ID="storeRole" runat="server">
        <Reader>
            <ext:JsonReader IDProperty="RoleId">
                <Fields>
                    <ext:RecordField Name="RoleId" />
                    <ext:RecordField Name="RoleName" />
                    <ext:RecordField Name="Description" />
                    <ext:RecordField Name="IsActive" />
                    <ext:RecordField Name="CreatedDate" />
                    <ext:RecordField Name="CreatedBy" />
                    <ext:RecordField Name="UpdatedDate" />
                    <ext:RecordField Name="UpdatedBy" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <ext:Store ID="storeMain" runat="server" OnRefreshData="storeMain_OnRefreshDate"
        AutoLoad="false" GroupField="ModuleName" WarningOnDirty="false">
        <Proxy>
            <ext:PageProxy />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="ScreenId">
                <Fields>
                    <ext:RecordField Name="ScreenId" />
                    <ext:RecordField Name="ScreenName" />
                    <ext:RecordField Name="RoleId" />
                    <ext:RecordField Name="ModuleName" />
                    <ext:RecordField Name="DisplayOrder" />
                    <ext:RecordField Name="IsAdd" />
                    <ext:RecordField Name="IsView" />
                    <ext:RecordField Name="IsDelete" />
                    <ext:RecordField Name="IsEdit" />
                    <ext:RecordField Name="IsPrint" />
                    <ext:RecordField Name="IsImport" />
                    <ext:RecordField Name="IsExport" />
                </Fields>
            </ext:JsonReader>
        </Reader>
        <Listeners>
            <Load Handler="CheckDataAll();" />
        </Listeners>
    </ext:Store>
    <ext:Viewport ID="Viewport1" runat="server" Layout="border">
        <Items>
            <ext:GridPanel ID="grdMain" runat="server" StripeRows="true" TrackMouseOver="true"
                AutoExpandColumn="ScreenName" Border="false" Region="Center" StoreID="storeMain"
                ColumnLines="true" EnableColumnMove="False">
                <TopBar>
                    <ext:Toolbar ID="tbMain" runat="server">
                        <Items>
                            <%-- <ext:Button ID="btnToggleGroups" runat="server" Text="Open/Minimize" Icon="TableSort">
                                <Listeners>
                                    <Click Handler="#{grdMain}.getView().toggleAllGroups();" />
                                </Listeners>
                            </ext:Button>--%>
                            <ext:ToolbarSpacer />
                            <ext:ToolbarFill />
                            <ext:ComboBox ID="cboRole" FieldLabel="Role" runat="server" Width="300"
                                ValueField="RoleId" DisplayField="RoleName" StoreID="storeRole">
                                <Listeners>
                                    <Select Handler="#{storeMain}.load();" />
                                </Listeners>
                            </ext:ComboBox>
                            <ext:ToolbarSpacer />
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:RowNumbererColumn />
                        <ext:Column ColumnID="ModuleName" DataIndex="ModuleName" Header="Module" />
                        <ext:Column ColumnID="ScreenName" DataIndex="ScreenName" Header="Screen" MenuDisabled="true"
                            Sortable="false" />
                        <ext:CheckColumn Editable="true" ColumnID="IsView" DataIndex="IsView" Header="View<br/><input type='checkbox' id='chkViewAll' name='addAlll' onclick='setViewAll()'/>"
                            MenuDisabled="true" Sortable="false" Align="Center" Width="50" />
                        <ext:CheckColumn Editable="true" ColumnID="IsAdd" DataIndex="IsAdd" Header="Add<br/><input type='checkbox' id='chkAddAll' name='addAlll' onclick='setAddAll()'/>"
                            MenuDisabled="true" Sortable="false" Align="Center" Width="50" />
                        <ext:CheckColumn Editable="true" ColumnID="IsEdit" DataIndex="IsEdit" Header="Edit<br/><input type='checkbox' id='chkEditAll' name='addAlll' onclick='setEditAll()'/>"
                            MenuDisabled="true" Sortable="false" Align="Center" Width="50" />
                        <ext:CheckColumn Editable="true" ColumnID="IsDelete" DataIndex="IsDelete" Header="Delete<br/><input type='checkbox' id='chkDeleteAll' name='addAlll' onclick='setDeleteAll()'/>"
                            MenuDisabled="true" Sortable="false" Align="Center" Width="50" />
                        <ext:CheckColumn Editable="true" ColumnID="IsPrint" DataIndex="IsPrint" Header="Print<br/><input type='checkbox' id='chkPrintAll' name='addAlll' onclick='setPrintAll()'/>"
                            MenuDisabled="true" Sortable="false" Align="Center" Width="50" />
                        <ext:CheckColumn Editable="true" ColumnID="IsImport" DataIndex="IsImport" Header="Import<br/><input type='checkbox' id='chkImportAll' name='addAlll' onclick='setImportAll()'/>"
                            MenuDisabled="true" Sortable="false" Align="Center" Width="50" />
                        <ext:CheckColumn Editable="true" ColumnID="IsExport" DataIndex="IsExport" Header="Export<br/><input type='checkbox' id='chkExportAll' name='addAlll' onclick='setExportAll()'/>"
                            MenuDisabled="true" Sortable="false" Align="Center" Width="50" />
                    </Columns>
                </ColumnModel>
                <SelectionModel>
                    <ext:RowSelectionModel ID="RowSelectionModel1" runat="server" SingleSelect="false" />
                </SelectionModel>
                <View>
                    <ext:GroupingView ID="GroupingView1" HideGroupedColumn="true" runat="server" EnableRowBody="true"
                        MarkDirty="false">
                    </ext:GroupingView>
                </View>
                <DirectEvents>
                    <AfterEdit OnEvent="grdMain_OnAfterEdit">
                        <ExtraParams>
                            <ext:Parameter Name="ScreenId" Value="e.record.data.ScreenId" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="RoleId" Value="e.record.data.RoleId" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="IsAdd" Value="e.record.data.IsAdd" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="IsView" Value="e.record.data.IsView" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="IsDelete" Value="e.record.data.IsDelete" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="IsEdit" Value="e.record.data.IsEdit" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="IsPrint" Value="e.record.data.IsPrint" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="IsImport" Value="e.record.data.IsImport" Mode="Raw">
                            </ext:Parameter>
                            <ext:Parameter Name="IsExport" Value="e.record.data.IsExport" Mode="Raw">
                            </ext:Parameter>
                        </ExtraParams>
                    </AfterEdit>
                </DirectEvents>
                <Listeners>
                    <BeforeEdit Fn="CheckEdit" />
                    <AfterEdit Fn="setViews" />
                    <Update Handler="alert('2');" />
                    <RowContextMenu Handler="e.preventDefault(); #{RowContextMenu}.dataRecord = this.store.getAt(rowIndex); #{RowContextMenu}.showAt(e.getXY()); if(grdMain.selModel.selections.items.length<2) {this.getSelectionModel().selectRow(rowIndex);}" />
                </Listeners>
                <LoadMask ShowMask="true" />
            </ext:GridPanel>
        </Items>
    </ext:Viewport>
    <ext:Menu ID="RowContextMenu" runat="server">
        <Items>
            <ext:MenuItem ID="mmCheckAll" runat="server" Text="Check All" Icon="Accept">
                <Listeners>
                    <Click Handler="CheckAllRows(#{grdMain},true);" />
                </Listeners>
            </ext:MenuItem>
            <ext:MenuItem ID="mmUnCheckAll" runat="server" Text="Uncheck All" Icon="Cancel">
                <Listeners>
                    <Click Handler="CheckAllRows(#{grdMain},false);" />
                </Listeners>
            </ext:MenuItem>
        </Items>
    </ext:Menu>
    </form>
</body>
</html>
